project(
    'nnps',
    'fortran',
    version: '1.5.2409',
    license: 'MIT',
    meson_version: '>=0.50.0',
    default_options: [
        'buildtype=release',
        'fortran_std=f2008',
        'default_library=both',
    ],
)

fargs = []

if host_machine.system() == 'windows'
    if meson.get_compiler('fortran').get_id() != 'gcc'
        fargs += ['/heap-arrays:100000000']
    endif
else
    if meson.get_compiler('fortran').get_id() != 'gcc'
        fargs += ['-heap-arrays 100000000']
    endif
endif

subdir('src')

nnps_lib = library(
    meson.project_name(),
    sources: files(filenames),
    version: meson.project_version(),
    install: true,
    dependencies: dependency('openmp'),
    fortran_args: fargs,
)

subdir('test')

nnps_test_lib = library(
    'nnps_test',
    nnps_test_src,
    install: false,
)

# tests and benchmarks

pause = get_option('_pause')
if pause
    fargs += ['-DPAUSE=' + pause.to_string()]
endif

executable(
    'bench2du-grid',
    'test/bench2du-grid.F90',
    link_with: [nnps_test_lib, nnps_lib],
    fortran_args: fargs,
)

executable(
    'bench2dn-grid',
    'test/bench2dn-grid.F90',
    link_with: [nnps_test_lib, nnps_lib],
    fortran_args: fargs,
)

executable(
    'bench3du-grid',
    'test/bench3du-grid.F90',
    link_with: [nnps_test_lib, nnps_lib],
    fortran_args: fargs,
)

executable(
    'bench3dn-grid',
    'test/bench3dn-grid.F90',
    link_with: [nnps_test_lib, nnps_lib],
    fortran_args: fargs,
)

executable(
    'bench2du-grid_is',
    'test/bench2du-grid_is.F90',
    link_with: [nnps_test_lib, nnps_lib],
    fortran_args: fargs,
)

executable(
    'bench2dn-grid_is',
    'test/bench2dn-grid_is.F90',
    link_with: [nnps_test_lib, nnps_lib],
    fortran_args: fargs,
)

executable(
    'bench3du-grid_is',
    'test/bench3du-grid_is.F90',
    link_with: [nnps_test_lib, nnps_lib],
    fortran_args: fargs,
)

executable(
    'bench3dn-grid_is',
    'test/bench3dn-grid_is.F90',
    link_with: [nnps_test_lib, nnps_lib],
    fortran_args: fargs,
)

executable(
    'bench2du',
    'test/bench2du.f90',
    link_with: [nnps_test_lib, nnps_lib],
    fortran_args: fargs,
)

executable(
    'bench2dn',
    'test/bench2dn.f90',
    link_with: [nnps_test_lib, nnps_lib],
    fortran_args: fargs,
)

executable(
    'bench3du',
    'test/bench3du.f90',
    link_with: [nnps_test_lib, nnps_lib],
    fortran_args: fargs,
)

executable(
    'bench3dn',
    'test/bench3dn.f90',
    link_with: [nnps_test_lib, nnps_lib],
    fortran_args: fargs,
)

# examples

executable(
    'example_direct1d',
    'example/example_direct1d.f90',
    link_with: [nnps_lib, nnps_test_lib],
)

executable(
    'example_direct2d',
    'example/example_direct2d.f90',
    link_with: [nnps_lib, nnps_test_lib],
)

executable(
    'example_direct3d',
    'example/example_direct3d.f90',
    link_with: [nnps_lib, nnps_test_lib],
)

executable(
    'example_grid1d',
    'example/example_grid1d.f90',
    link_with: [nnps_lib, nnps_test_lib],
)

executable(
    'example_grid2d',
    'example/example_grid2d.f90',
    link_with: [nnps_lib, nnps_test_lib],
)

executable(
    'example_grid3d',
    'example/example_grid3d.f90',
    link_with: [nnps_lib, nnps_test_lib],
)

executable(
    'example_grid2d_is',
    'example/example_grid2d_is.f90',
    link_with: [nnps_lib, nnps_test_lib],
)

executable(
    'example_grid3d_is',
    'example/example_grid3d_is.f90',
    link_with: [nnps_lib, nnps_test_lib],
)

executable(
    'example_tree1d',
    'example/example_tree1d.f90',
    link_with: [nnps_lib, nnps_test_lib],
)

executable(
    'example_tree2d',
    'example/example_tree2d.f90',
    link_with: [nnps_lib, nnps_test_lib],
)

executable(
    'example_tree3d',
    'example/example_tree3d.f90',
    link_with: [nnps_lib, nnps_test_lib],
)


nnps_inc = nnps_lib.private_dir_include()
nnps_dep = declare_dependency(link_with: nnps_lib, include_directories: nnps_inc)


if host_machine.system() == 'windows'
    symbols_file = 'lib' + meson.project_name() + '-' + meson.project_version().split(
        '.',
    )[0] + '.dll.symbols'
    obj_suffix = '.obj'
else
    symbols_file = 'lib' + meson.project_name() + '.so.' + meson.project_version() + '.symbols'
    obj_suffix = '.o'
endif

nnps_obj = []
foreach source : filenames
    nnps_obj += source.replace('/', '_') + obj_suffix
endforeach

install_subdir(
    nnps_lib.full_path() + '.p',
    install_dir: 'include' / meson.project_name(),
    strip_directory: true,
    exclude_files: [
        'depscan.dd',
        meson.project_name() + '-deps.json',
        symbols_file,
        meson.project_name() + '.dat',
        nnps_obj,
    ],
)


nnps_lic = files('LICENSE')
install_data(
    nnps_lic,
    install_dir: join_paths(
        get_option('prefix'),
        'share',
        'licenses',
        meson.project_name(),
    ),
)

pkg = import('pkgconfig')
pkg.generate(
    name: meson.project_name(),
    description: 'NNPS scheme practice for particle simulation.',
    version: meson.project_version(),
    libraries: nnps_lib,
    subdirs: meson.project_name(),
)


